import Vue from 'vue'
import Vuex from 'vuex'
import $axios from '@/plugins/axios'
// import _ from 'lodash';
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    locationCity:"",
    city:'北京',
    cityid:1,
    cts:[],
    locationData:null,
    seatMessage:[],
    ticket:[],
  },
  getters:{
    // city(state){
    //   return state.city;
    // },
    locationData(state){
      return state.locationData;
    },
    cityid(state){
      state.cts.forEach(el => {
        if(el.nm == state.city){
          state.cityid=el.id;
        }
      });
      return state.cityid;
    },
    seatMessage(state){
      return state.seatMessage;
    }
  },
  mutations: {
    saveCitiesDetial(state,detial){
      state.city=detial.city;
      state.locationCity=detial.city;
      state.locationData=detial;
    },
    saveCts(state,cts){
      state.cts=cts;
      cts.forEach(el => {
        if(el.nm == state.city){
          state.cityid=el.id;
        }
      });
    },
    resetLocation(state,detial){
      state.locationCity=detial.city;
      state.locationData=detial;
    },
    setdata(state){
      state.locationCity='定位失败，请稍后重试';
      state.locationData=null;
    },
    cityChange(state,city){
      state.city=city;
      state.cts.forEach(el => {
        if(el.nm == state.city){
          state.cityid=el.id;
        }
      });
    },
    buy(state,message){
      let li=state.seatMessage;
      // let b=_.uniqWith(li, _.isEqual);
      if (li.length>0) {
        state.seatMessage.forEach((item,index) =>{
          if(item.cn == message.cn && item.mn == message.mn && item.tm == message.tm){
            li.splice(index,1,message);
          }else{
            li.push(message);
          }
        });
        state.seatMessage=li;
      }else{
        state.seatMessage.push(message);
      }
    },
    saveTicket(state,message){
      state.ticket.push(message);
    },
  },
  actions: {
    getgeolocation(context){
      navigator.geolocation.getCurrentPosition(function(position){
        let lat=position.coords.latitude;
        let long=position.coords.longitude;
        $axios.get(`city/latlng?lat=${lat}&lng=${long}`).then(re=>{
          context.commit("saveCitiesDetial",re.data.data);
        })
      },function( err ){
        switch(err.code) {
          case err.PERMISSION_DENIED:
            console.log( "用户拒绝对获取地理位置的请求。");
            break;
          case err.POSITION_UNAVAILABLE:
            console.log( "位置信息是不可用的。");
            break;
          case err.TIMEOUT:
            console.log( "请求用户地理位置超时。");
            break;
          case err.UNKNOWN_ERROR:
            console.log( "未知错误。");
            break;
        }
      })
    },
    resetgeolocation(context){
      navigator.geolocation.getCurrentPosition(function(position){
        let lat=position.coords.latitude;
        let long=position.coords.longitude;
        $axios.get(`city/latlng?lat=${lat}&lng=${long}`).then(re=>{
          context.commit("resetLocation",re.data.data);
        })
      },function( err ){
        context.commit("setdata");
        switch(err.code) {
          case err.PERMISSION_DENIED:
            console.log( "用户拒绝对获取地理位置的请求。");
            break;
          case err.POSITION_UNAVAILABLE:
            console.log( "位置信息是不可用的。");
            break;
          case err.TIMEOUT:
            console.log( "请求用户地理位置超时。");
            break;
          case err.UNKNOWN_ERROR:
            console.log( "未知错误。");
            break;
        }
      })
    },
    getcities(context){
      $axios.get('cities.json').then(re=>{
        context.commit("saveCts",re.data.cts);
      })
    },
  },
  modules: {
  }
})
